package ngat.oss;

import ngat.message.OSS.*;
import ngat.util.logging.*;
import ngat.phase2.*; 
import ngat.instrument.*;
import ngat.phase2.nonpersist.*;

import java.util.*;
import java.io.*;

import jyd.dbms.*;
import jyd.storable.*;
import jyd.collection.*;

/**
 *
 * @author S.N.Fraser
 */
public class GET_USERImpl extends TransactionImpl {

    /** The path used to identify the USER in the OSS database. */
    protected Path userPath;

    /** Create a GET_USERImpl for the specified request.*/
    public GET_USERImpl(GET_USER request) {
	super(request);
	this.userPath = request.getUserPath();
    }

    /**
     * Retrieves the USER specified.  
     */
    public TRANSACTION_DONE exec() { 

	myLog = LogManager.getLogger("TRACE");

	User user = null;
	
	Transaction t = null;
	try {
	    t = Transaction.current();
	    myLog.log(1, CLASS, id, "exec", 
		      "Fetching USER: "+userPath);
	    user = null;
	    try {
		user = OSS_Utilities.fetchUser(userPath);
		myLog.log(1, CLASS, id, "exec", "USER was fetched ok");
	    } catch (Exception iox) {
		myLog.log(1, CLASS, id, "exec",
			   "USER: "+userPath+
			   " Error fetching USER from Database: "+iox);
		return error(TRANSACTION.DATABASE_READ_ERROR,			   
				     "USER: "+userPath+
				     ": Error reading USER from database: "+iox);
	    }		
	    
	    // Check USER exists.
	    if (user == null) {
		// Couldn't find the USER.
		myLog.log(1, CLASS, id, "exec", 
			   "User: "+userPath+" Not in database.");
		return error(TRANSACTION.PATH_RESOLUTION_ERROR, 
			     "Could not find User: "+userPath);
	    } 

	    // Done
	    GET_USER_DONE done = new GET_USER_DONE(id);
	    done.setSuccessful(true);
	    done.setUser(user);
	    return done;	    

	} finally {
	    t.commit();
	}	
    }
    
} // Class Def. [GET_USERImpl].



